#include "File.h"

boolean FileExists(char *Filename)
{
	FILE *File = fopen(Filename, "r");
	if(File == NULL)
		return False;
	fclose(File);
	return True;
}

boolean DirectoryExists(char *DirectoryName)
{
	struct stat S;
	if(stat(DirectoryName, &S)==0)
		return True;
	return False;
}

int FileModifiedTime(char *Filename)
{
	struct stat Info;
	if(stat(Filename, &Info)!=0)
		return -1;
	return Info.st_mtime;
}

int FileSize(char *Filename)
{
	struct stat Info;
	if(stat(Filename, &Info)!=0)
		return -1;
	return Info.st_size;
}

int GetEntireFile(char *Filename, char *Buffer)
{
	char C;
	int Index = 0;
	FILE *FileHandle = fopen(Filename, "r");
	if(FileHandle == NULL)
		return -1;
	while(!feof(FileHandle) && !ferror(FileHandle))
	{
		/* char Tmp[100];
			 fgets(Tmp, 100, FileHandle);*/
		C = getc(FileHandle);
		Buffer[Index++] = C;
	}
	if(ferror(FileHandle))
		return -1;
	Buffer[Index-1] = '\0';
	return Index;
}

int SetEntireFile(char *Name, char *Data, int Size)
{
	FILE *FileHandle = fopen(Name, "w");
	if(FileHandle == NULL)
		return -1;
	int Written = fwrite(Data, sizeof(char), Size, FileHandle);
	if(Written != Size)
		return -1;
	return 0;
}

#ifdef TEST
int main()
{
	SetEntireFile("TEST", "TEST", 5);
	printf("%i", DirectoryExists("TEST"));
	DeleteFile("TEST");
}
#endif /* TEST */
